/*
 * Name: $RCSfile: StringUtility.java,v $
 * Version: $Revision: 1.1 $
 * Date: $Date: Oct 31, 2011 1:54:00 PM $
 *
 * Copyright (C) 2011 COMPANY_NAME, Inc. All rights reserved.
 */

package com.toidep.utilities;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import android.text.Html;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.widget.EditText;

/**
 * StringUtility class
 * 
 * @author Lemon
 * 
 */
public final class StringUtility {
//	private String TAG = getClass().getSimpleName();
	/**
	 * Check Edit Text input string
	 * 
	 * @param editText
	 * @return
	 */
	public static boolean isEmpty(EditText editText) {
		if (editText == null
				|| editText.getEditableText() == null
				|| editText.getEditableText().toString().trim()
						.equalsIgnoreCase("")) {
			return true;
		}
		return false;
	}

	/**
	 * Check input string
	 * 
	 * @param editText
	 * @return
	 */
	public static boolean isEmpty(String editText) {
		if (editText == null || editText.trim().equalsIgnoreCase("")) {
			return true;
		}
		return false;
	}

	/**
	 * Merge all elements of a string array into a string
	 * 
	 * @param strings
	 * @param separator
	 * @return
	 */
	public static String join(String[] strings, String separator) {
		StringBuffer sb = new StringBuffer();
		int max = strings.length;
		for (int i = 0; i < max; i++) {
			if (i != 0)
				sb.append(separator);
			sb.append(strings[i]);
		}
		return sb.toString();
	}

	/**
	 * Convert current date time to string
	 * 
	 * @param updateTime
	 * @return
	 */
	public static String convertNowToFullDateString() {
		SimpleDateFormat dateformat = new SimpleDateFormat(
				"yyyy-MM-dd HH:mm:ss");
		dateformat.setTimeZone(TimeZone.getTimeZone("GMT"));

		Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
		return dateformat.format(calendar.getTime());
	}

	/**
	 * Initial sync date string
	 * 
	 * @return
	 */
	public static String initDateString() {
		return "1900-01-01 09:00:00";
	}

	/**
	 * Convert a string divided by ";" to multiple xmpp users
	 * 
	 * @param userString
	 * @return
	 */
	public static String[] convertStringToXmppUsers(String userString) {
		return userString.split(";");
	}
	public static String convertStreamToString(InputStream is) throws UnsupportedEncodingException {

//	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
	    StringBuilder sb = new StringBuilder();

	    String line = null;
	    try {
	        while ((line = reader.readLine()) != null) {
	            sb.append((line + "\n"));
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    } 
//	    finally {
//	        try {
//	            is.close();
//	        } catch (IOException e) {
//	            e.printStackTrace();
//	        }
//	    }
	    
	    return sb.toString();
	}
	public static String encode(String value) throws UnsupportedEncodingException{
		return URLEncoder.encode(value, "UTF-8");
	}
	
	public static String enCryptMD5(String s){
		 try {
		        java.security.MessageDigest md = java.security.MessageDigest.getInstance("MD5");
		        byte[] array = md.digest(s.getBytes());
		        StringBuffer sb = new StringBuffer();
		        for (int i = 0; i < array.length; ++i) {
		          sb.append(Integer.toHexString((array[i] & 0xFF) | 0x100).substring(1,3));
		       }
		        return sb.toString();
		    } catch (java.security.NoSuchAlgorithmException e) {
		    }
		    return null;
	}
	
	public static String formatNumberWithComma(String number) {
		
		double amount = Double.parseDouble(number);
		DecimalFormat formatter = new DecimalFormat("#,###");
		return formatter.format(amount);
		
	}

	public static String convertVietnameseToAscii(String input) {
		
		input = input.replace("A","A");
		input = input.replace("a","a");
		input = input.replace("Ã„â€š","A");
		input = input.replace("Ã„Æ’","a");
		input = input.replace("Ãƒâ€š","A");
		input = input.replace("ÃƒÂ¢","a");	
		input = input.replace("E","E");
		input = input.replace("e","e");
		input = input.replace("ÃƒÅ ","E");
		input = input.replace("ÃƒÂª","e");
		input = input.replace("I","I");
		input = input.replace("i","i");
		input = input.replace("O","O");
		input = input.replace("o","o");
		input = input.replace("Ãƒâ€�","O");
		input = input.replace("ÃƒÂ´","o");
		input = input.replace("Ã†Â ","O");
		input = input.replace("Ã†Â¡","o");
		input = input.replace("U","U");
		input = input.replace("u","u");
		input = input.replace("Ã†Â¯","U");
		input = input.replace("Ã†Â°","u");
		input = input.replace("Y","Y");
		input = input.replace("y","y");
		
		input = input.replace("Ãƒâ‚¬","A");
		input = input.replace("ÃƒÂ ","a");
		input = input.replace("Ã¡ÂºÂ°","A");
		input = input.replace("Ã¡ÂºÂ±","a");
		input = input.replace("Ã¡ÂºÂ¦","A");
		input = input.replace("Ã¡ÂºÂ§","a");	
		input = input.replace("ÃƒË†","E");
		input = input.replace("ÃƒÂ¨","e");
		input = input.replace("Ã¡Â»â‚¬","E");
		input = input.replace("Ã¡Â»ï¿½","e");
		input = input.replace("ÃƒÅ’","I");
		input = input.replace("ÃƒÂ¬","i");
		input = input.replace("Ãƒâ€™","O");
		input = input.replace("ÃƒÂ²","o");
		input = input.replace("Ã¡Â»â€™","O");
		input = input.replace("Ã¡Â»â€œ","o");
		input = input.replace("Ã¡Â»Å“","O");
		input = input.replace("Ã¡Â»ï¿½","o");
		input = input.replace("Ãƒâ„¢","U");
		input = input.replace("ÃƒÂ¹","u");
		input = input.replace("Ã¡Â»Âª","U");
		input = input.replace("Ã¡Â»Â«","u");
		input = input.replace("Ã¡Â»Â²","Y");
		input = input.replace("Ã¡Â»Â³","y");

		input = input.replace("Ãƒï¿½","A");
		input = input.replace("ÃƒÂ¡","a");
		input = input.replace("Ã¡ÂºÂ®","A");
		input = input.replace("Ã¡ÂºÂ¯","a");
		input = input.replace("Ã¡ÂºÂ¤","A");
		input = input.replace("Ã¡ÂºÂ¥","a");	
		input = input.replace("Ãƒâ€°","E");
		input = input.replace("ÃƒÂ©","e");
		input = input.replace("Ã¡ÂºÂ¾","E");
		input = input.replace("Ã¡ÂºÂ¿","e");
		input = input.replace("Ãƒï¿½","I");
		input = input.replace("ÃƒÂ­","i");
		input = input.replace("Ãƒâ€œ","O");
		input = input.replace("ÃƒÂ³","o");
		input = input.replace("Ã¡Â»ï¿½","O");
		input = input.replace("Ã¡Â»â€˜","o");
		input = input.replace("Ã¡Â»Å¡","O");
		input = input.replace("Ã¡Â»â€º","o");
		input = input.replace("ÃƒÅ¡","U");
		input = input.replace("ÃƒÂº","u");
		input = input.replace("Ã¡Â»Â¨","U");
		input = input.replace("Ã¡Â»Â©","u");
		input = input.replace("Ãƒï¿½","Y");
		input = input.replace("ÃƒÂ½","y");

		input = input.replace("Ã¡ÂºÂ¢","A");
		input = input.replace("Ã¡ÂºÂ£","a");
		input = input.replace("Ã¡ÂºÂ²","A");
		input = input.replace("Ã¡ÂºÂ³","a");
		input = input.replace("Ã¡ÂºÂ¨","A");
		input = input.replace("Ã¡ÂºÂ©","a");	
		input = input.replace("Ã¡ÂºÂº","E");
		input = input.replace("Ã¡ÂºÂ»","e");
		input = input.replace("Ã¡Â»â€š","E");
		input = input.replace("Ã¡Â»Æ’","e");
		input = input.replace("Ã¡Â»Ë†","I");
		input = input.replace("Ã¡Â»â€°","i");
		input = input.replace("Ã¡Â»Å½","O");
		input = input.replace("Ã¡Â»ï¿½","o");
		input = input.replace("Ã¡Â»â€�","O");
		input = input.replace("Ã¡Â»â€¢","o");
		input = input.replace("Ã¡Â»Å¾","O");
		input = input.replace("Ã¡Â»Å¸","o");
		input = input.replace("Ã¡Â»Â¦","U");
		input = input.replace("Ã¡Â»Â§","u");
		input = input.replace("Ã¡Â»Â¬","U");
		input = input.replace("Ã¡Â»Â­","u");
		input = input.replace("Ã¡Â»Â¶","Y");
		input = input.replace("Ã¡Â»Â·","y");	
		
		input = input.replace("ÃƒÆ’","A");
		input = input.replace("ÃƒÂ£","a");
		input = input.replace("Ã¡ÂºÂ´","A");
		input = input.replace("Ã¡ÂºÂµ","a");
		input = input.replace("Ã¡ÂºÂª","A");
		input = input.replace("Ã¡ÂºÂ«","a");	
		input = input.replace("Ã¡ÂºÂ¼","E");
		input = input.replace("Ã¡ÂºÂ½","e");
		input = input.replace("Ã¡Â»â€ž","E");
		input = input.replace("Ã¡Â»â€¦","e");
		input = input.replace("Ã„Â¨","I");
		input = input.replace("Ã„Â©","i");
		input = input.replace("Ãƒâ€¢","O");
		input = input.replace("ÃƒÂµ","o");
		input = input.replace("Ã¡Â»â€“","O");
		input = input.replace("Ã¡Â»â€”","o");
		input = input.replace("Ã¡Â»Â ","O");
		input = input.replace("Ã¡Â»Â¡","o");
		input = input.replace("Ã…Â¨","U");
		input = input.replace("Ã…Â©","u");
		input = input.replace("Ã¡Â»Â®","U");
		input = input.replace("Ã¡Â»Â¯","u");
		input = input.replace("Ã¡Â»Â¸","Y");
		input = input.replace("Ã¡Â»Â¹","y");	
		
		input = input.replace("Ã¡ÂºÂ ","A");
		input = input.replace("Ã¡ÂºÂ¡","a");
		input = input.replace("Ã¡ÂºÂ¶","A");
		input = input.replace("Ã¡ÂºÂ·","a");
		input = input.replace("Ã¡ÂºÂ¬","A");
		input = input.replace("Ã¡ÂºÂ­","a");	
		input = input.replace("Ã¡ÂºÂ¸","E");
		input = input.replace("Ã¡ÂºÂ¹","e");
		input = input.replace("Ã¡Â»â€ ","E");
		input = input.replace("Ã¡Â»â€¡","e");
		input = input.replace("Ã¡Â»Å ","I");
		input = input.replace("Ã¡Â»â€¹","i");
		input = input.replace("Ã¡Â»Å’","O");
		input = input.replace("Ã¡Â»ï¿½","o");
		input = input.replace("Ã¡Â»Ëœ","O");
		input = input.replace("Ã¡Â»â„¢","o");
		input = input.replace("Ã¡Â»Â¢","O");
		input = input.replace("Ã¡Â»Â£","o");
		input = input.replace("Ã¡Â»Â¤","U");
		input = input.replace("Ã¡Â»Â¥","u");
		input = input.replace("Ã¡Â»Â°","U");
		input = input.replace("Ã¡Â»Â±","u");
		input = input.replace("Ã¡Â»Â´","Y");
		input = input.replace("Ã¡Â»Âµ","y");
		
		input = input.replace("Ã„ï¿½","D");
		input = input.replace("Ã„â€˜","d");
		
		return input; 
		
	}
	
	/**
	 * Convert 1 chuÃ¡Â»â€”i string cÃƒÂ³ 1 hoÃ¡ÂºÂ·c nhiÃ¡Â»ï¿½u kÃƒÂ½ tÃ¡Â»Â± liÃ¡Â»ï¿½n nhau Ã„â€˜Ã†Â°Ã¡Â»Â£c gÃ¡ÂºÂ¡ch chÃƒÂ¢n sang dÃ¡ÂºÂ¡ng SpannableString Ã„â€˜Ã¡Â»Æ’ show lÃƒÂªn TextView
	 * @author freshtea  
	 * @param input
	 * @return
	 */
	public static SpannableString convertStringUnderline(String input) {
		String htmlString = convertHTMLtoString(input);
		
		int start = htmlString.indexOf("<u>");
		int end = htmlString.indexOf("</u>");
		
		if (start == -1) {
			return new SpannableString(htmlString);
		}
		
//		htmlString.replaceAll("<u>", "");
//		htmlString.replaceAll("\\</u>", "");
		
		String str = htmlString.substring(0, start);
		str += htmlString.substring(start + 3, end);
		str += htmlString.substring(end + 4);
		
		end -= 3;
		
		SpannableString content = new SpannableString(str);
		content.setSpan(new UnderlineSpan(), start, end, 0);
		 
		return content;
	}

	/**
	 * Convert time from long type to String follow format mm:ss
	 * @author freshtea
	 * @param timeLoss
	 * @return
	 */
	public static String convertTimeToString(long timeLoss) {
		int minute = (int) (timeLoss / 60);
		int second = (int) (timeLoss % 60);
		
		return (minute < 10 ? "0" : "") + minute + ":" +(second < 10 ? "0" : "")+ second;
	}

	/**
	 * Convert time from long type to String follow format mm'ss"
	 * @author freshtea
	 * @param timeLoss
	 * @return
	 */
	public static String convertTimeToString2(long timeLoss) {
		int minute = (int) (timeLoss / 60);
		int second = (int) (timeLoss % 60);
		
		return (minute < 10 ? "0" : "") + minute + "'" +(second < 10 ? "0" : "")+ second + "\"";
	}
	
	public static String convertHTMLtoString(String input){
		return Html.fromHtml(input).toString();
	}

	public static String convertIntToString(int value) {
		DecimalFormat df = new DecimalFormat("###,###,###,###.##");
		return df.format(value);
	}

	public static String convertLongToString(long value) {
		DecimalFormat df = new DecimalFormat("###,###,###,###.##");
		return df.format(value);
	}

	public static String convertDoubleToString(double value) {
		DecimalFormat df = new DecimalFormat("###,###,###,###.##");
		return df.format(value);
	}

	public static String parseImageUrl(String stringValue) {
		return stringValue.replace(" ", "%20").replace("(", "%28").replace(")", "%29");
	}

	public static String resizeImageUrl(String stringValue, int width, int height) {
		int index = stringValue.lastIndexOf(".");
		
		if (index != -1)
			return stringValue.substring(0, index) + "." + width + "x" + height + stringValue.substring(index);
		
		return stringValue;
	}
}
